<?php

//
//  Copyright (C) 2004-2006  Autodesk, Inc.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of version 2.1 of the GNU Lesser
//  General Public License as published by the Free Software Foundation.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

?>

<?php
session_start();

require_once("config.php");
require_once(ROOT_DIR_CLASES.'/cCliente.php');
require_once(ROOT_DIR_CLASES.'/cLogin.php');
require_once(ROOT_DIR_CLASES.'/cAuditoria.php');


class QueryLogin
{
	private $args = null;

	function __construct($args)
	{
		$this->args = $args;
	}

	function Execute()
	{
		// CREAMOS UN LOGGER
		//$LOGGER = new PHPLogger(LOGGER_PATH, LOGGER_LEVEL);
		// PASAMOS LOS ARGUMENTOS A VARIABLES LOCALES
		$codigorc = $this->args['codigorc'];
		$codigocuenta = $this->args['codigocuenta'];
		$particion = $this->args['particion'];
		$password = strtoupper(md5($this->args['password']));
		$objetivo = $this->args['objetivo'];
		$codigoFlota = $this->args['codigoFlota'];
		$alias = $this->args['aliasString'];
		$codigoDealer = $this->args['codigoDealer'];
		//--------------------------------------

		// INSTANCIAMOS UN OBJETO CLOGIN Y LE SETEAMOS SUS ATRIBUTOS EN BASE A LOS ARGUMENTOS PASADOS
		$loginClass = new CLogin();
		$loginClass->codigorc = $codigorc;
		$loginClass->codigocuenta = $codigocuenta;
		$loginClass->particion = $particion;
		$loginClass->password = $password;
		$loginClass->objetivo = $objetivo;
		$loginClass->codigoFlota = $codigoFlota;
		$loginClass->alias = $alias;
		$loginClass->codigoDealer = $codigoDealer;

		// SI EL USUARIO NO SE ENCUENTRA YA LOGUEADO
		if( !isset($_SESSION['cliente']) )
		{
			// LE PEDIMOS A BYKOMCLIENTE QUE VERIFIQUE EL USUARIO
			$loginClass = BykomCliente::LoginInitialValidation($loginClass);

			// SI EL LOGUEO NO PRODUJO NINGUN ERROR, ES DECIR, SE LOGUEO CORRECTAMENTE,
			// SETEAMOS LAS VARIABLES DE SESION CORRESPONDIENTES
			if( $loginClass->errorCode == 0 )
			{
				$_SESSION['cliente'] = session_id();
				$_SESSION['isWebCliente'] = 1; //indicamos que entramos a traves de "Web-Cliente"
				$_SESSION['ultfecha'] = $loginClass->ultimaFecha;
				$_SESSION['objetivo'] = $loginClass->objetivoDB;
				$_SESSION['objetivoReal'] = $loginClass->objetivo;
				$_SESSION['uniqueID']= $loginClass->cuentaFk;
				$_SESSION['tablaID'] = $loginClass->tablaId;
				$_SESSION['monitoreoWeb']= $loginClass->monitoreoWeb;
				$_SESSION['labelLogin']= $loginClass->getLabelLogin();
				$_SESSION['usuariosLogueados'] = $loginClass->usuariosLogueados;

				$_SESSION['loginClass'] = serialize($loginClass);

				// AGREGAMOS EL EVENTO A LA AUDITORIA
				$auditoria = new CAuditoria();
				$auditoria->tablaId = $loginClass->cuentaFk;
				$auditoria->objetivo = $loginClass->objetivo;
				$auditoria->tipoAuditoria = 1; /*Acceso*/
				$auditoria->tipoVinculo = 1; /*Web*/
				$auditoria->fechaYHora = date("Y-m-d H:i:s");
				$auditoria->ip = HOSTCLIENT_NO_SSL;
				$auditoria->genericFk = "null";
				$auditoria = $auditoria->persistir( $auditoria );
			}
		}
		else
		{
			$loginClass->errorCode = 50;	// ERRORCODE 50: SESION YA INICIADA LOCALMENTE
		}

		// YA SEA QUE EL LOGUEO FUE SATISFACTORIO O NO, DEVOLVEMOS UN OBJETO CLOGIN
		return $loginClass;
	}
}
?>